<?php
if (!defined("MCTDESIGN")) die("# ");
if (isset($_POST['delete']) || isset($_POST['flag']) && $_POST['flag'] == 1) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

 	for($i=1;$i<=$total;$i++){

		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';

		if (!empty($checkbox[$i])) {

            $anyDB->query = "delete from ".$tb_fix."_hotels where hos_hca_id = '".$checkbox[$i]."'";
			$anyDB->query("DELETE FROM ".$tb_fix."_hotel_categorys WHERE hca_id='".$checkbox[$i]."'");
		}

	}

	$html_body = _html_trans("Xóa Danh mục thành công !");

	return false;

}
if(isset($_GET['del']) && $_GET['del'] == 'true'){
    $id = intval($_GET['id']);
    $sql = "select * from ".$tb_fix."_hotel_images where him_hos_id = '$id'";
    $q = $anyDB->query($sql);
    if($anyDB->numRows() > 0){
        $r = $anyDB->getAll($q);
        foreach ($r as $val){
            $imglink = $val['him_pic'];
            @unlink($imglink);        
        }
    }
    $anyDB->query("DELETE FROM ".$tb_fix."_hotel_images WHERE him_hos_id='$id'");
	$anyDB->query("DELETE FROM ".$tb_fix."_hotels WHERE hos_id='$id'");
    $html_body = _html_trans("Xóa khách sạn thành công !","admin.php?obj=Hotels&act=list");

	return false;             
}
if (isset($_POST['delete_more_hotels'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

 	for($i=1;$i<=$total;$i++){

		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';

		if (!empty($checkbox[$i])) {
		    $id =$checkbox[$i];
            $sql = "select * from ".$tb_fix."_hotel_images where him_hos_id = '$id'";
            $q = $anyDB->query($sql);
            if($anyDB->numRows() > 0){
                $r = $anyDB->getAll($q);
                foreach ($r as $val){
                    $imglink = $val['him_pic'];
                    @unlink($imglink);        
                }
            } 
			$anyDB->query("DELETE FROM ".$tb_fix."_hotel_images WHERE him_hos_id='$id'");
	        $anyDB->query("DELETE FROM ".$tb_fix."_hotels WHERE hos_id='$id'");
		}

	}

	$html_body = _html_trans("Xóa khách sạn thành công !","admin.php?obj=Hotels&act=list");

	return false;

}
if (isset($_POST['order'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

    for ($i=1;$i<=$total;$i++)

      {

           $cat_id    = isset($_POST["order_id_$i"])    ? $_POST["order_id_$i"]    : 0;

           $cat_order = isset($_POST["order_$i"]) ? $_POST["order_$i"] : 0;

           if ( ($cat_id>0)&&($cat_order>0) )

           {		

				$mysql->query("UPDATE ".$tb_fix."_hotel_categorys SET hca_order='$cat_order' WHERE hca_id='".$cat_id."'");

           }

    }

	$html_body = _html_trans("Đã cập nhật thứ tự các Danh mục !");

	return false;

}


if (isset($_POST['add'])) {
    $cat_tit = fixquotes(stripslashes($_POST["hca_title"]));
    $cat_id = (int)$_POST['hca_id'];    
    $cat_desc  = fixquotes(stripslashes($_POST["hca_des"]));    	
	if (empty($cat_tit)) {

		$html_body = _html_trans("Bạn phải nhập tên Danh mục !");

		return false;

	}
    $isEdit = $_POST['isEdit'];
    
    if($isEdit){ // EDIT
        $catID = $_POST['cat_id'];
        $sql = "update ".$tb_fix."_hotel_categorys set hca_title = '$cat_tit', hca_parent_id = '$cat_id',hca_desc='$cat_desc' where hca_id = '$catID'";
        //echo $sql; die();
    }else{
        $sql = "insert into ".$tb_fix."_hotel_categorys 
                values (0,'$cat_tit','$cat_desc','$cat_id',0,1)";    
    }
    $q = $anyDB->query($sql);
    if($q){
        if($isEdit){
            $html_body = _html_trans(" Đã sửa Danh mục !");    
        }else{
            $html_body = _html_trans("Thêm Danh mục mới thành công !");
        }
            
    }else{
        $html_body = _html_trans("Xảy ra lỗi. Vui lòng liên hệ Webmaster !");
    }
	

	return false;

}

function _html_addCategory()

{

	global $dir_tpl, $mysql, $tb_fix, $temp;

	$temp_body = $temp;

	$temp_body->assign( 'base_tpl', "".$dir_tpl."" );

	$_html = $temp_body->fetch( '_adm_admin_add.mct' );

	return $_html;

}

function _html_manageCategory()

{

	global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$_c,$temp,$anyDB;

	$temp_body = $temp;
    // show list tour for add form
    $sql = "SELECT * FROM ".$tb_fix."_hotel_categorys where hca_parent_id = 0 ORDER BY hca_order ASC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll();
    foreach($row as $k => $v){
        $hcaid = $v['hca_id'];
        $sql = "SELECT * FROM ".$tb_fix."_hotel_categorys where hca_parent_id = '$hcaid' ORDER BY hca_order ASC";
        $q = $anyDB->query($sql);
        $t = $anyDB->numRows($q);
        if($t > 0){
            $row1 = $anyDB->getAll();
            foreach($row1 as $key => $val){
                $row[$k]['Child'][] = $val;
            }    
        }
        
    }
   //echo "<pre>";print_r($row);die();
    $temp_body->assign("cate",$row);
    // Danh sach tour
    $goParentTour = isset($_GET["parentCat"]) ? fixquotes(stripslashes($_GET["parentCat"])) : '0';
    $temp_body->assign("pTour",$goParentTour);
    $sql = "select * from ".$tb_fix."_hotel_categorys where hca_parent_id = '$goParentTour'  and hca_status = 1 order by hca_order asc";
    $q = $anyDB->query($sql);
    $t = $anyDB->numRows($q);
    if($t>0){
        $r = $anyDB->getAll($q);
        foreach($r as $k=>$v){
            // show num parent tour
            $hca_id = $v['hca_id'];
            $sql = "select count(hca_id) as total_parent from ".$tb_fix."_hotel_categorys where hca_parent_id = '$hca_id'";            
            $q = $anyDB->query($sql);
            $num = $anyDB->getNext($q);            
            $r[$k]['numParent'] = $num['total_parent'];
        }
        
        $temp_body->assign("listtour",$r);        
    }
    $temp_body->assign("breakcum",breakcum("Quản lý danh mục Khách sạn "));
	$_html = $temp_body->fetch( '_adm_hotels_menu.mct' );

	return $_html;

}

function _html_addKhachSan(){
    global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$temp,$anyDB,$temp;
    $temp->assign("dir_tpl",$dir_tpl);
    // show list tour for add form
    $sql = "SELECT * FROM ".$tb_fix."_hotel_categorys where hca_parent_id = 0 ORDER BY hca_order ASC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll();
    foreach($row as $k => $v){
        $hcaid = $v['hca_id'];
        $sql = "SELECT * FROM ".$tb_fix."_hotel_categorys where hca_parent_id = '$hcaid' ORDER BY hca_order ASC";
        $q = $anyDB->query($sql);
        $t = $anyDB->numRows($q);
        if($t > 0){
            $row1 = $anyDB->getAll();
            foreach($row1 as $key => $val){
                $row[$k]['Child'][] = $val;
            }    
        }
        
    }
   //echo "<pre>";print_r($row);die();
    $temp->assign("cate",$row);
    
    $temp->assign("breakcum",breakcum("Thêm Khách sạn mới"));
	$_html = $temp->fetch( '_adm_hotel_add.mct' );
	return $_html;
}
function reArrayFiles(&$file_post) {
    // sap xep lai mang $_FILE khi su dung multi upload
    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}
if(isset($_POST['add_hotel'])){
    $isEdit = $_POST['IsEdit'];
    $urlReturn = "";
    if($isEdit){
        $h_id = (int)$_POST['h_id'];
        $urlReturn = "admin.php?obj=Hotels&act=edit&id=$h_id";
    }    
    $h_cat = $_POST['h_cate'];
    $h_title = fixquotes(stripslashes($_POST["h_title"]));
    $h_desc = fixquotes(stripslashes($_POST["h_desc"]));
    $h_content = fixquotes(stripslashes($_POST["h_content"]));
    $h_star = isset($_POST['h_star']) ? $_POST['h_star']:0;
    $h_km = isset($_POST['h_km']) ? $_POST['h_km']:0;
    // check
    if($h_cat == 0) {$html_body = _html_trans(" Chưa chọn danh mục khách sạn !",$urlReturn); return false;}
    if(empty($h_title)) {$html_body = _html_trans(" Chưa nhập tên khách sạn !",$urlReturn); return false;}
    if(empty($h_desc)) {$html_body = _html_trans(" Chưa nhập giới thiệu tóm tắt !",$urlReturn); return false;}
    if(empty($h_content)) {$html_body = _html_trans(" Chưa nhập giới thiệu chi tiết !",$urlReturn); return false;}
    
    // get cate id + parent id
    if($isEdit){
        $sql = "select hca_parent_id from ".$tb_fix."_hotel_categorys where hca_id = '$h_cat'";
        $q = $anyDB->query($sql);
        $r = $anyDB->getNext($q);
        $root_cat = $r['hca_parent_id'];
    }else{
        $h_cat_new = explode("|",$h_cat);
        $h_catid = $h_cat_new[0];
        $h_cat_parent_id = $h_cat_new[1];    
    }
    
    // insert hotel
    if($isEdit){
        $sql = "update ".$tb_fix."_hotels 
                        set 
                            hos_title='$h_title', 
                            hos_desc='$h_desc', 
                            hos_content='$h_content',
                            hos_hca_id='$h_cat',
                            hos_hca_root_id='$root_cat',
                            hos_star = '$h_star',
                            hos_sale_off = '$h_km'
                        where hos_id = '$h_id'";   
    }else{
        $sql = "insert into ".$tb_fix."_hotels values (0,'$h_title','$h_desc','$h_content','$h_catid','$h_cat_parent_id','$h_star','$h_km',1)";    
    }
    $q = $anyDB->query($sql);
    if($q) {
        if($isEdit){
            $h_inserted_id = $h_id;
        }else{
            $h_inserted_id = $anyDB->getInsertId();    
        }        
        $file_ary = reArrayFiles($_FILES['h_img']);
        test($file_ary,1);
        foreach ($file_ary as $file) {
            $fileName =  $file['name'];
            $fileTmpName = $file['tmp_name'];
            if(!empty($fileName)){
                $post_img = @uploadIMG($fileName,$fileTmpName,"hotels");
                $sql = "insert into ".$tb_fix."_hotel_images values (0,'$h_inserted_id','$post_img')"; 
                $anyDB->query($sql);    
            }
        }
        if($isEdit){
            $html_body =_html_trans(" Cập nhật khách sạn thành công!",'?obj=Hotels&act=list');    
        }else{
            $html_body =_html_trans(" Thêm khách sạn thành công!",'?obj=Hotels&act=list');    
        }
    }
    
    return false;
}

function _html_listHotels(){
    global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$temp,$anyDB,$temp;
    $temp->assign("breakcum",breakcum("Danh sách khách sạn"));
    $temp->assign("dir_tpl",$dir_tpl);
     // phân trang
        $pagegroup_size = 10;
        $page=_POST('p');
        if (!$page||$page==0){$page=1;}
        $limit=_POST('limit');
        if($limit==''||$limit>30) {$limit=10;}
        $offset=($page-1)*$limit;
        $li="limit $offset,$limit";
        $key_word = isset($_GET['key_word']) ? $_GET['key_word']:'';
        $s_ksname = isset($_GET['s_ksname']) ? fixquotes(stripslashes($_GET['s_ksname'])) :'';
        $star = isset($_GET['Star']) ? fixquotes(stripslashes($_GET['Star'])) :0;
        $sale = isset($_GET['Sale']) ? fixquotes(stripslashes($_GET['Sale'])) :0;
        $temp->assign("star",$star);
        //$temp->assign("sale",$sale);
        $temp->assign("s_name",$s_ksname);
        $temp->assign("k_word",$key_word);
        $where = "";
        if($s_ksname) $where .= " and h.hos_title like '%$s_ksname%'";
        if ($star) $where .= " and h.hos_star = '$star'";
        //if ($sale) $where .= " and h.hos_sale_off = '$sale'";
        if($key_word) $where.=" and h.hos_hca_id='$key_word'";
        
        $sql = "select count(h.hos_id) as total_r from ".$tb_fix."_hotels as h
                        left join ".$tb_fix."_hotel_categorys as hm
                            on h.hos_hca_id = hm.hca_id
                            $where
                            ";
         $q = $anyDB->query($sql);
         $arr = $anyDB->getNext($q);
         $totalRecord = $arr['total_r']; // tong so ban ghi
         //echo $sql;
         $sql = "select h.*,hm.hca_title,hm.hca_id from ".$tb_fix."_hotels as h
                        left join ".$tb_fix."_hotel_categorys as hm
                            on h.hos_hca_id = hm.hca_id
                            where 1=1
                            $where
                            order by h.hos_id desc
                            $li
                            ";
        $q = $anyDB->query($sql);
        $r = $anyDB->getAll($q);
        $c = 0;
        foreach($r as $k=>$v){
            $hstar = $v['hos_star'];
            for($i=0;$i<$hstar;$i++){                
                $r[$k]['star'][$c] = $c;
                $c++;
            }
            
        }
        $temp->assign("tou",$r);
        $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"?obj=Hotels&act=list&key_word=$key_word");
        $temp->assign('pages',$pages);
        $temp->assign('base_url',$site_path);
	$_html = $temp->fetch( '_adm_hotel_list.mct' );
	return $_html; 
}
function _html_editHotel($id){
    global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$temp,$anyDB,$temp;
    $temp->assign("dir_tpl",$dir_tpl);
    // show list tour for add form
    $sql = "SELECT * FROM ".$tb_fix."_hotel_categorys where hca_parent_id = 0 ORDER BY hca_order ASC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll();
    foreach($row as $k => $v){
        $hcaid = $v['hca_id'];
        $sql = "SELECT * FROM ".$tb_fix."_hotel_categorys where hca_parent_id = '$hcaid' ORDER BY hca_order ASC";
        $q = $anyDB->query($sql);
        $t = $anyDB->numRows($q);
        if($t > 0){
            $row1 = $anyDB->getAll();
            foreach($row1 as $key => $val){
                $row[$k]['Child'][] = $val;
            }    
        }
        
    }
   //echo "<pre>";print_r($row);die();
    $temp->assign("cate",$row);
    // info tour
    $sql = "select * from ".$tb_fix."_hotels where hos_id = '$id' ";
    $q = $anyDB->query($sql);
    $t = $anyDB->numRows($q);
    if($t > 0){
        $r = $anyDB->getNext($q);
        $tour_title = $r['hos_title'];
        $hos_id = $r['hos_id'];
        $sql = "select * from ".$tb_fix."_hotel_images where him_hos_id = '$hos_id'";
        $q = $anyDB->query($sql);
        if($anyDB->numRows($q) > 0){
            $row = $anyDB->getAll($q);
            foreach($row as $k=>$v){
                $r['images'][] = $v;
            }
        }
        //echo "<pre>";print_r($r);die();
        $temp->assign("hos",$r); 
        $temp->assign("breakcum",breakcum("Sửa khách sạn: $tour_title"));
    }else{
        $html_body =_html_trans(" Không tìm thấy Khách sạn nào!");
    }
    
    $temp->assign('base_url',$site_path);
	$_html = $temp->fetch( '_adm_hotel_edit.mct' );
	return $_html; 
}
switch($act){
    case "add":
        $html_body = _html_addKhachSan();
        break;
    case "list":
        $html_body = _html_listHotels();
        break;  
    case "edit":
        $html_body = _html_editHotel($id);
        break;              
    default:
        $html_body = _html_manageCategory();
        break;    
}
/**
 * desc: them, sua, xoa hotel
 * finished: 22:40 16/10/2011
 * author: Hoang Dung
 */
?>